package com.yonyou.crm.bpub.approval.ctrl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.bpub.approval.entity.ActionVO;
import com.yonyou.crm.bpub.approval.entity.DjTypeEnum;
import com.yonyou.crm.bpub.approval.entity.HistoryVO;
import com.yonyou.crm.bpub.approval.entity.ProcessModelVO;
import com.yonyou.crm.bpub.approval.rmitf.IProcessRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;

@RequestMapping("/bpub/approvals")
@RestController
public class ProcessCtrl {

	@Autowired
	private IProcessRmService service;
	
	@RequestMapping(value="/djtypes", method = RequestMethod.GET)
	public Object djTypeList(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		for(DjTypeEnum djTypeEnum : DjTypeEnum.values()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", djTypeEnum.getValue());
			map.put("name", djTypeEnum.getName());
			list.add(map);
		}
		
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("list", list);
		return ResponseUtil.getObject(rsMap);
	}
	
	/*
	 * 同步部门和人员数据
	 */
	@RequestMapping(value="/syn", method = RequestMethod.GET)
	public Object synDeptsAndUsers(HttpServletRequest request, HttpServletResponse response) {
		service.synDeptsAndUsers();
		return ResponseUtil.getObject();
	}
	
	/*
	 * 创建流程模型
	 */
	@RequestMapping(value="/processes", method = RequestMethod.POST)
	public Object createProcess(HttpServletRequest request, HttpServletResponse response) {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		int djType = paramObject.getInt("djType");
		paraMap.put("djType", djType);
		
		if (paramObject.containsKey("bizTypeId")) {
			Long bizTypeId = paramObject.getLong("bizTypeId");
			paraMap.put("bizTypeId", bizTypeId);
		} else {
			paraMap.put("bizTypeId", 0L);
		}
		String rs = service.createProcess(paraMap);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("ysUrl", rs);
		return ResponseUtil.getObject(rsMap);
	}
	
	/*
	 * 查询流程模型列表
	 */
	@RequestMapping(value="/processes", method = RequestMethod.GET)
	public Object getProcessModelList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		if (paramObject.containsKey("djType")) {
			String djTypeStr = paramObject.getString("djType");
			if (!djTypeStr.isEmpty()) {
				paraMap.put("djType", Integer.parseInt(djTypeStr));
			}
		}
		if (paramObject.containsKey("bizTypeId")) {
			String bizTypeStr = paramObject.getString("bizTypeId");
			if (!bizTypeStr.isEmpty()) {
				paraMap.put("bizTypeId", Long.parseLong(bizTypeStr));
			}
		}
		
		List<ProcessModelVO> list = service.getProcessModelList(paraMap);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 删除流程模型
	 */
	@RequestMapping(value="/processes", method = RequestMethod.DELETE)
	public Object deleteProcessModel(HttpServletRequest request, HttpServletResponse response) {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String ysModelId = paramObject.getString("ysModelId");
		service.deleteProcessModel(ysModelId);
		return ResponseUtil.getObject();
	}
	
	/*
	 * 查询已完成的审批单据
	 */
	@RequestMapping(value="/finished", method = RequestMethod.GET)
	public Object getFinished(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("page", paramObject.getInt("page"));
		paraMap.put("pageSize", paramObject.getInt("pageSize"));
		paraMap.put("queryDateKey", paramObject.get("queryDateKey"));//搜索时间范围
		paraMap.put("queryKey", paramObject.get("queryKey"));//模糊查询
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("datalist", service.getFinished(paraMap));
		
		List<Map<String, String>> titleList = new ArrayList<Map<String,String>>();
		
		Map<String, String> titleMap = new HashMap<String, String>();
		titleMap.put("key", "1");
		titleMap.put("dataIndex", "name");
		titleMap.put("title", "任务主题");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "2");
		titleMap.put("dataIndex", "approvalUserList");
		titleMap.put("title", "最后审批人");
		titleMap.put("type", "array");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "3");
		titleMap.put("dataIndex", "completeTime");
		titleMap.put("title", "审批时间");
		titleMap.put("type", "date");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "4");
		titleMap.put("dataIndex", "statusName");
		titleMap.put("title", "审核历史");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		map.put("titlelist", titleList);
		
		return ResponseUtil.getObject(map);
	}
	/*
	 * 查询未完成的审批单据
	 */
	@RequestMapping(value="/notfinished", method = RequestMethod.GET)
	public Object getNotFinished(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("page", paramObject.getInt("page"));
		paraMap.put("pageSize", paramObject.getInt("pageSize"));
		paraMap.put("queryDateKey", paramObject.get("queryDateKey"));//搜索时间范围
		paraMap.put("queryKey", paramObject.get("queryKey"));//模糊查询
		
		
		
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("datalist", service.getNotFinished(paraMap));
		
		List<Map<String, String>> titleList = new ArrayList<Map<String,String>>();
		
		Map<String, String> titleMap = new HashMap<String, String>();
		titleMap.put("key", "1");
		titleMap.put("dataIndex", "name");
		titleMap.put("title", "任务主题");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "2");
		titleMap.put("dataIndex", "approvalUserList");
		titleMap.put("title", "待审人");
		titleMap.put("type", "array");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "3");
		titleMap.put("dataIndex", "stayTimeLength");
		titleMap.put("title", "停留时长");
		titleMap.put("type", "string");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "4");
		titleMap.put("dataIndex", "statusName");
		titleMap.put("title", "审核状态");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		map.put("titlelist", titleList);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 查询流程实例
	 */
	@RequestMapping(value="/details", method = RequestMethod.GET)
	public <T extends Serializable> Object getDetail(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Long businessId = paramObject.getLong("id");  
		int djType = paramObject.getInt("type");
		T rs = service.getDetail(businessId, djType);
		return ResponseUtil.getObject(rs);
	}
	
	/*
	 * 查询审批动作列表
	 */
	@RequestMapping(value="/actions", method = RequestMethod.GET)
	public Object getActions(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String instanceId = paramObject.getString("instanceid");
		String taskId = null;
		if (paramObject.containsKey("taskid")) {
			taskId = paramObject.getString("taskid");
			if (taskId.isEmpty()) {
				taskId = null;
			}
		}
		
		List<ActionVO> list = service.getActions(instanceId, taskId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("actionlist", list);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 执行任务
	 */
	@RequestMapping(value="/actions", method = RequestMethod.POST)
	public Object actionTask(HttpServletRequest request, HttpServletResponse response) {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Long businessId = paramObject.getLong("id");
		int djType = paramObject.getInt("type");
		String instanceId = paramObject.getString("instanceid");
		String taskId = null;
		if (paramObject.containsKey("taskid")) {
			taskId = paramObject.getString("taskid");
			if (taskId.isEmpty()) {
				taskId = null;
			}
		}
		String comment = "";
		if (paramObject.containsKey("comment")) {
			comment = paramObject.getString("comment");
		}
		String action = paramObject.getString("action");
		
		service.actionTask(businessId, djType, instanceId, taskId, comment, action);
		return ResponseUtil.getObject();
	}
	
	/*
	 * 查询审批历史
	 */
	@RequestMapping(value="/histories", method = RequestMethod.GET)
	public Object getHistoryTasks(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Long businessId = paramObject.getLong("id");
		int djType = paramObject.getInt("type");
		
		List<HistoryVO> rs = service.getHistoryTasks(businessId, djType, "desc");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("historylist", rs);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 查询审批历史(用于PC的详情页上方显示)
	 */
	@RequestMapping(value="/details/histories", method = RequestMethod.GET)
	public Object getHistoryTasksForDetailPage(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Long businessId = paramObject.getLong("id");
		int djType = paramObject.getInt("type");
		
		List<HistoryVO> list = service.getHistoryTasks(businessId, djType, "asc");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<HistoryVO> doneList = new ArrayList<HistoryVO>();
		List<HistoryVO> todoList = new ArrayList<HistoryVO>();
		
		for (HistoryVO historyVO : list) {
			if (historyVO.getStatus().equals("提交")) {
				map.put("commit", historyVO);
				continue;
			}
			if (historyVO.getStatus().equals("待审")) {
				todoList.add(historyVO);
				continue;
			}
			doneList.add(historyVO);
			continue;
		}
		map.put("done", doneList);
		map.put("todo", todoList);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 获取待办任务列表
	 */
	@RequestMapping(value="/todo", method = RequestMethod.GET)
	public Object getTasksTodo(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("page", paramObject.getInt("page"));
		paraMap.put("pageSize", paramObject.getInt("pageSize"));
		paraMap.put("queryDateKey", paramObject.get("queryDateKey"));//搜索时间范围
		paraMap.put("queryKey", paramObject.get("queryKey"));//模糊查询
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("datalist", service.getTasksTodo(paraMap));
		
		List<Map<String, String>> titleList = new ArrayList<Map<String,String>>();
		
		Map<String, String> titleMap = new HashMap<String, String>();
		titleMap.put("key", "1");
		titleMap.put("dataIndex", "name");
		titleMap.put("title", "任务主题");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "2");
		titleMap.put("dataIndex", "stayTimeLength");
		titleMap.put("title", "停留时长");
		titleMap.put("type", "string");
		titleList.add(titleMap);
		
		map.put("titlelist", titleList);
		
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 获取已办任务列表
	 */
	@RequestMapping(value="/done", method = RequestMethod.GET)
	public Object getTasksDone(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("page", paramObject.getInt("page"));
		paraMap.put("pageSize", paramObject.getInt("pageSize"));
		paraMap.put("queryDateKey", paramObject.get("queryDateKey"));//搜索时间范围
		paraMap.put("queryKey", paramObject.get("queryKey"));//模糊查询
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("datalist", service.getTasksDone(paraMap));
		
		List<Map<String, String>> titleList = new ArrayList<Map<String,String>>();
		
		Map<String, String> titleMap = new HashMap<String, String>();
		titleMap.put("key", "1");
		titleMap.put("dataIndex", "name");
		titleMap.put("title", "任务主题");
		titleMap.put("type", "link");
		titleList.add(titleMap);
		
		titleMap = new HashMap<String, String>();
		titleMap.put("key", "2");
		titleMap.put("dataIndex", "completeTime");
		titleMap.put("title", "审批时间");
		titleMap.put("type", "date");
		titleList.add(titleMap);
		
		map.put("titlelist", titleList);
		
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 获取提交单据总数
	 */
	@RequestMapping(value="/notfinishedtotal", method = RequestMethod.GET)
	public Object getProcessInstancesCount(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		int total = service.getProcessInstancesCount(false);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		return ResponseUtil.getObject(map);
	}
	
	/*
	 * 获取待办任务总数
	 */
	@RequestMapping(value="/todototal", method = RequestMethod.GET)
	public Object getTasksTodoCount(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		int total = service.getTasksCount(false);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		return ResponseUtil.getObject(map);
	}
	
	@RequestMapping(value="/remind", method = RequestMethod.POST)
	public Object remind(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Long businessId = paramObject.getLong("id");
		int djType = paramObject.getInt("type");
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> personlist = (List<Map<String, Object>>)paramObject.get("personlist");
		List<String> personids = new ArrayList<String>();
		for (Map<String, Object> map : personlist) {
			personids.add(map.get("id").toString());
		}
		if (personids.size() == 0) {
			throw new CrmMessageException("参数错误");
		}
		service.remind(businessId, djType, personids);
		return ResponseUtil.getObject("提醒成功");
	}
	/**
	 * 发送消息，由云审批系统调用
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/sendmessages", method = RequestMethod.POST)
	public Object sendMessageForProcess(HttpServletRequest request, HttpServletResponse response){
		JSONObject paramObject = RequestUtil.getParamObject(request);
		return null;
	}
}
